﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.Devices;
using System.Threading.Tasks;
using System.Xml;

namespace ReName2File
{
    class Program
    {
        static void Main(string[] args)
        {
            //string csproj = System.Configuration.ConfigurationManager.AppSettings["csproj"];
            //XMLOpHelper xmldoc = new XMLOpHelper(csproj, "http://schemas.microsoft.com/developer/msbuild/2003");
            //var nodes = xmldoc.GetXmlNodeListByXpath("/ns:Project/ns:ItemGroup/ns:Compile/@Include");
            //xmldoc.CreateXmlNodeByXPath2("/ns:Project/ns:ItemGroup/ns:Compile", "Compile", "", "Include", "test");
            string entitys = System.Configuration.ConfigurationManager.AppSettings["entitys"];
            string entitysTo = System.Configuration.ConfigurationManager.AppSettings["entitysTo"];
            string bll = System.Configuration.ConfigurationManager.AppSettings["bll"];
            string bllTo = System.Configuration.ConfigurationManager.AppSettings["bllTo"];
            string ibll = System.Configuration.ConfigurationManager.AppSettings["ibll"];
            string ibllTo = System.Configuration.ConfigurationManager.AppSettings["ibllTo"];
            string dal = System.Configuration.ConfigurationManager.AppSettings["dal"];
            string dalTo = System.Configuration.ConfigurationManager.AppSettings["dalTo"];
            string idal = System.Configuration.ConfigurationManager.AppSettings["idal"];
            string idalTo = System.Configuration.ConfigurationManager.AppSettings["idalTo"];

            Task task = new Task(() =>
            {
                bool result = false;
                if (!string.IsNullOrEmpty(entitys))
                {
                    Console.WriteLine("->正在重命名 Entitys......");
                    result = ReNameFiles(entitys, "entitys");
                    if (result)
                    {
                        Console.WriteLine("->完成重命名 Entitys......");

                        Console.WriteLine("->正在移动文件 Entitys......");
                        MoveTo(entitys, entitysTo);
                        Console.WriteLine("->完成移动文件 Entitys......");
                    }
                }

                if (!string.IsNullOrEmpty(bll))
                {
                    Console.WriteLine("->正在重命名 bll......");
                    result = ReNameFiles(bll, "bll");
                    if (result)
                    {
                        Console.WriteLine("->完成重命名 bll......");

                        Console.WriteLine("->正在移动文件 bll......");
                        MoveTo(bll, bllTo);
                        Console.WriteLine("->完成移动文件 bll......");
                    }
                }

                if (!string.IsNullOrEmpty(ibll))
                {
                    Console.WriteLine("->正在重命名 ibll......");
                    result = ReNameFiles(ibll, "ibll");
                    if (result)
                    {
                        Console.WriteLine("->完成重命名 ibll......");

                        Console.WriteLine("->正在移动文件 ibll......");
                        MoveTo(ibll, ibllTo);
                        Console.WriteLine("->完成移动文件 ibll......");
                    }
                }

                if (!string.IsNullOrEmpty(dal))
                {
                    Console.WriteLine("->正在重命名 dal......");
                    result = ReNameFiles(dal, "dal");
                    if (result)
                    {
                        Console.WriteLine("->完成重命名 dal......");
                        Console.WriteLine("->正在移动文件 dal......");
                        MoveTo(dal, dalTo);
                        Console.WriteLine("->完成移动文件 dal......");
                    }
                }

                if (!string.IsNullOrEmpty(idal))
                {
                    Console.WriteLine("->正在重命名 idal......");
                    result = ReNameFiles(idal, "idal");
                    if (result)
                    {
                        Console.WriteLine("->完成重命名 idal......");
                        Console.WriteLine("->正在移动文件 idal......");
                        MoveTo(idal, idalTo);
                        Console.WriteLine("->完成移动文件 idal......");
                    }
                }

            });
            task.Start();
            Console.ReadLine();
        }

        static bool ReNameFiles(string pathsDirectory, string rType)
        {
            bool result = false;
            try
            {
                if (!Directory.Exists(pathsDirectory))
                {
                    Console.WriteLine("文件路径不存在：" + pathsDirectory);
                }
                else
                {
                    var files = Directory.GetFiles(pathsDirectory, "*.cs");
                    foreach (var item in files)
                    {
                        string filename = item.Split('\\').LastOrDefault();
                        if (string.IsNullOrEmpty(filename)) continue;
                        filename = filename.Split('.').FirstOrDefault();
                        string newName = Helper.ToPascalCase(filename);
                        switch (rType)
                        {
                            case "bll":
                                newName += "Service";
                                break;
                            case "ibll":
                                newName = "I" + newName;
                                break;
                            case "dal":
                                newName += "Dal";
                                break;
                            case "idal":
                                newName = "I" + newName + "Dal";
                                break;
                            case "entitys":
                                newName += "s";
                                break;
                        }
                        FileOperateProxy.ReNameFile(item, newName);
                        Console.WriteLine(string.Format("{0}->{1}......", filename, newName));
                        result = true;
                    }
                    if (rType == "entitys")
                    {
                        var efiles = Directory.GetFiles(pathsDirectory, "*.cs");
                        foreach (var item in efiles)
                        {
                            string filename = item.Split('\\').LastOrDefault();
                            if (string.IsNullOrEmpty(filename)) continue;
                            filename = filename.Split('.').FirstOrDefault();
                            //string newName = Helper.ToPascalCase(filename);
                            filename = filename.Substring(0, filename.Length - 1);
                            FileOperateProxy.ReNameFile(item, filename);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            return result;
        }

        static void MoveTo(string pathsDirectory, string moveToPath)
        {
            try
            {
                var nPathArray = moveToPath.Split('\\').ToList();
                nPathArray.RemoveAt(nPathArray.Count - 1);
                string newmoveToPath = string.Join("\\", nPathArray);
                newmoveToPath += "\\gt";

                var moveFiles = Directory.GetFiles(pathsDirectory, "*.cs");
                string info = string.Empty;
                if (!Directory.Exists(newmoveToPath)) Directory.CreateDirectory(newmoveToPath);
                var result = FileOperateProxy.MoveFiles(moveFiles, newmoveToPath, false, false, false, ref info);
                //if (result > 0)
                {
                    //修改工程包含新文件
                    moveFiles.ToList().ForEach(c =>
                    {
                        var fileName = c.Split('\\').LastOrDefault();
                        XMLOpHelper xmldoc = new XMLOpHelper(moveToPath, "http://schemas.microsoft.com/developer/msbuild/2003");
                        var nodes = xmldoc.GetXmlNodeListByXpath("/ns:Project/ns:ItemGroup/ns:Compile/@Include");
                        bool isInclude = false;
                        foreach (XmlNode item in nodes)
                        {
                            isInclude = item.Value.IndexOf(fileName) > 0;
                            if (isInclude) break;
                        }
                        if (!isInclude)
                        {
                            string nPath = "gt\\" + fileName;
                            xmldoc.CreateXmlNodeByXPath2("/ns:Project/ns:ItemGroup/ns:Compile", "Compile", "", "Include", nPath);
                        }
                    });

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

        }
    }
}
